using System;
using System.Collections.Generic;
using System.Text;
using Musicman.Entities.BusinessEntities;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Musicman.Common;

namespace Musicman.DAL
{
    public static class UserActivityDAO
    {
        #region Select 

        public static UserActivity GetUserActivity(long personID)
        {
            string sql = String.Format("SELECT NUMLOGSWEB,NUMLOGSCLIENT,TOTALFILES,TOTALLENGTH FROM DBO.USERSTATS WHERE PERSONID = {0}", personID);
            UserActivity res = null;
            IDataReader reader = null;
            try
            {
                SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
                reader = db.ExecuteReader(CommandType.Text, sql);
                if (reader != null && reader.Read())
                {
                    res = new UserActivity();
                    res.PersonID = personID;
                    res.NumLogsWeb = reader.IsDBNull(0) ? 0 : reader.GetInt32(0);
                    res.NumLogsClient = reader.IsDBNull(1) ? 0 : reader.GetInt32(1);
                    res.TotalFiles = reader.IsDBNull(2) ? 0 : reader.GetInt64(2);
                    res.TotalTraffic = reader.IsDBNull(3) ? 0 : reader.GetInt64(3);
                }
            }
            catch (Exception ex)
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
                res = null;
                throw ex;
            }
            return res;
        }

        public static List<UserActivity> GetUserActivities(string top, string where, string orderby)
        {
            string sql = String.Format(@"SELECT {0} US.NUMLOGSWEB, US.NUMLOGSCLIENT, US.TOTALFILES, US.TOTALLENGTH,
U.PERSONID, U.FIRSTNAME, U.LASTNAME 
FROM DBO.USERSTATS US
JOIN DBO.USERS U ON US.PERSONID = U.PERSONID {1} {2}", top, where, orderby);
            List<UserActivity> res = new List<UserActivity>();
            IDataReader reader = null;
            try
            {
                SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.MusicmanConnectionString));
                reader = db.ExecuteReader(CommandType.Text, sql);
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        UserActivity entity = new UserActivity();
                        entity.NumLogsWeb = reader.IsDBNull(0) ? 0 : reader.GetInt32(0);
                        entity.NumLogsClient = reader.IsDBNull(1) ? 0 : reader.GetInt32(1);
                        entity.TotalFiles = reader.IsDBNull(2) ? 0 : reader.GetInt64(2);
                        entity.TotalTraffic = reader.IsDBNull(3) ? 0 : reader.GetInt64(3);
                        entity.PersonID = reader.IsDBNull(4) ? 0 : reader.GetInt64(4);
                        entity.FirstName = reader.IsDBNull(5) ? "" : reader.GetString(5);
                        entity.LastName = reader.IsDBNull(6) ? "" : reader.GetString(6);
                        res.Add(entity);
                    }
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
                res = null;
                throw ex;
            }
            return res;
        }

        #endregion
    }
}
